<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: spacecp_credit_usergroup.php 6790 2010-03-25 12:30:53Z cnteacher $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

$language = lang('forum/misc');
$permlang = $language;
unset($language);
$maingroup = $_G['group'];
$ptype = in_array($_G['gp_ptype'], array(0, 1, 2)) ? intval($_G['gp_ptype']) : 0;
foreach($_G['cache']['usergroups'] as $gid => $value) {
	$cachekey[] = 'usergroup_'.$gid;
}
loadcache($cachekey);
$grouplist = array();

foreach($_G['cache']['usergroups'] as $gid => $group) {
	$group['type'] = $group['type'] == 'special' && $_G['cache']['usergroup_'.$gid]['radminid'] ? 'specialadmin' : $group['type'];
	$grouplist[$group['type']][$gid] = $_G['cache']['usergroup_'.$gid];
	if($_G['cache']['usergroup_'.$gid]['radminid']) {
		$admingids[] = $gid;
	}
}

if($ptype == 1) {
	require_once libfile('function/forum');
	require_once libfile('function/forumlist');
	$perms = array('viewperm', 'postperm', 'replyperm', 'getattachperm', 'postattachperm');
	$fid = intval($_G['gp_fid']);
	if(!$fid) {
		$fid = DB::result(DB::query("SELECT fid FROM ".DB::table('forum_forum')." WHERE status='1' AND type='forum'"), 0);
	}
	$query = DB::query("SELECT fid, viewperm, postperm, replyperm, getattachperm, postattachperm FROM ".DB::table('forum_forumfield')." WHERE fid='$fid'");
	while($forum = DB::fetch($query)) {
		foreach($perms as $perm) {
			$groupids = $forum[$perm] ? explode("\t", $forum[$perm]) : array();
			foreach($_G['cache']['usergroups'] as $gid => $group) {
				if($forum[$perm] && in_array($gid, $groupids)) {
					$forumperm[$gid][$perm] = 1;
				} elseif($gid == 7 && ($perm == 'viewperm' || $perm == 'getattachperm') || $gid != 7) {
					$forumperm[$gid][$perm] = 1;
				}
			}
		}
	}
	$forumselect = "<select name=\"forumid\" onchange=\"showForumPerm(this.value)\" class=\"ps\" style=\"width:155px\">\n".str_replace('%', '%%', forumselect(FALSE, 0, $fid, TRUE, FALSE)).'</select>';

} elseif($ptype == 2) {
	$aperms = array('allowstickthread', 'allowdigestthread', 'allowbumpthread', 'allowhighlightthread', 'allowrecommendthread', 'allowstampthread', 'allowclosethread', 'allowmovethread', 'allowedittypethread', 'allowcopythread', 'allowmergethread', 'allowsplitthread', 'allowrepairthread', 'allowrefund', 'alloweditpoll', 'allowremovereward', 'alloweditactivity', 'allowedittrade', 'alloweditpost', 'allowwarnpost', 'allowbanpost', 'allowdelpost', 'allowviewreport', 'allowmodpost', 'allowmoduser', 'allowbanuser', 'allowbanip', 'allowedituser', 'allowmassprune', 'allowpostannounce', 'disablepostctrl', 'allowviewip');

	$query = DB::query("SELECT * FROM ".DB::table('common_admingroup')." WHERE admingid IN (".dimplode($admingids).")");
	while($group = DB::fetch($query)) {
		foreach($aperms as $perm) {
			$adminperm[$group['admingid']][$perm] = $group[$perm];
		}
	}
	if($adminperm[$maingroup['groupid']]) {
		$maingroup = array_merge($maingroup, $adminperm[$maingroup['groupid']]);
	}
} else {

	$bperms = array('allowvisit','readaccess','allowviewpro','allowinvisible','allowsearch','allownickname','allowcstatus');
	$pperms = array('allowpost','allowreply','allowpostpoll','allowvote','allowpostreward','allowpostactivity','allowpostdebate','allowposttrade','maxsigsize','allowsigbbcode','allowsigimgcode','maxbiosize','allowbiobbcode','allowbioimgcode','allowrecommend');
	$aperms = array('allowgetattach', 'allowpostattach', 'allowsetattachperm', 'maxattachsize', 'maxsizeperday', 'attachextensions');
	$sperms = array('allowblog', 'allowdoing', 'allowupload', 'allowshare', 'allowpoke', 'allowfriend', 'allowclick', 'allowmyop', 'allowcomment', 'allowstat', 'allowpostarticle');

	$allperms = array();
	$allkey = array_merge($bperms, $pperms, $aperms, $sperms);
	foreach($grouplist as $key => $groups) {
		foreach($groups as $gid => $group) {
			foreach($allkey as $id => $pkey) {
				$allperms[$pkey][$group['groupid']] = $group[$pkey];
			}
		}
	}

}
$extgroupids = $_G['member']['extgroupids'] ? explode("\t", $_G['member']['extgroupids']) : array();

$_G['gp_perms'] = 'member';
include_once template("home/spacecp_credit_usergroup");
?>
